package com.fzy.core.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import org.apache.commons.lang.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fzy.core.system.entity.PropertyWrapper;

import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class BuilderUtils {
	private static final Logger log = LoggerFactory.getLogger(BuilderUtils.class);
	private static long randomSeed = 0L;

	public static Configuration getFreemarkerEngine(String templatePath) {
		if (StringUtils.isBlank(templatePath))
			templatePath = "/ge-template";
		templatePath = templatePath.trim();
		Configuration freemarkerEngine = new Configuration();
		freemarkerEngine.setDefaultEncoding("UTF-8");
		TemplateLoader l = new ClassTemplateLoader(BuilderUtils.class,
				templatePath);
		freemarkerEngine.setTemplateLoader(l);
		return freemarkerEngine;
	}

	public static String produce(Configuration freemarkerEngine,
			String templateName, Object data) {
		StringWriter tempWriter = new StringWriter();
		BufferedWriter bw = new BufferedWriter(tempWriter);
		Template t;
		try {
			t = freemarkerEngine.getTemplate(templateName);
			t.process(data, bw);
		} catch (TemplateException e) {
			log.error(e.getMessage(), e);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
		try {
			bw.flush();
		} catch (IOException e) {
			log.error(e.getMessage(), e);
			throw new RuntimeException("Error while flushing to string", e);
		}
		return tempWriter.toString();
	}
	
	public static Object params2javaType(Object params, String javaType){
		Object result = params;
		if(params == null) return result;
		String[] x = (String[]) params;
		if(x.length == 1) result = x[0];
		if(PropertyWrapper.JAVA_LONG.equals(javaType))
			result = NumberUtils.createLongSafely((String)result);
		if(PropertyWrapper.JAVA_DOUBLE.equals(javaType))
			result = NumberUtils.createDoubleSafely((String)result);
		if(PropertyWrapper.JAVA_DATE.equals(javaType))
			result = DateUtils.parseDateSafely((String)result, DateUtils.PATTERN_SHOW_LONG);
		
		return result;
	}

	/**是否为保留字段
	 * @return
	 */
	public static boolean isSavedProperty(String propertyName){
		boolean result = false;
		String[] l = new String[]{"id", "createdUser", "createdDate", "updatedUser", "updatedDate", "status"};
		for (int i = 0; i < l.length; i++) {
			String s = l[i];
			if(s.equals(propertyName)){
				result = true;
				break;
			}
		}
		return result;
	}
	
	public static String obj2xmlAttr(Object o) {
		String result = "";
		List destFields = BeanUtils.getFields(o.getClass());

		for (Iterator iterator2 = destFields.iterator(); iterator2.hasNext();) {
			Field destField = (Field) iterator2.next();
			String fieldName = destField.getName();
			destField.setAccessible(true);
			try {
				String value = (String) destField.get(o);
				if (value != null) {
					result += " " + StringUtils.camel2hyphen(fieldName) + "=\"" + value
							+ "\"";
				}
			} catch (IllegalArgumentException e1) {
				e1.printStackTrace();
			} catch (IllegalAccessException e1) {
				e1.printStackTrace();
			}
		}
		return result;
	}

	public static String getRandomLong() {
		long seed = randomSeed++ + System.currentTimeMillis();
		Random r = new Random(seed);
		return r.nextLong() + "L";
	}
	
}
